package com.jason.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jason.seckill.pojo.SeckillOrder;
import com.jason.seckill.mapper.SeckillOrderMapper;
import com.jason.seckill.pojo.User;
import com.jason.seckill.service.ISeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.seckill.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jason
 * @since 2021-10-23
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Long getResult(User user, Long goodsId) {
        //没库存
        if (redisUtil.hasKey("isStockEmpty:" + goodsId)) {
            return -1L;
        }
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(
                new QueryWrapper<SeckillOrder>()
                        .eq("user_id", user.getId())
                        .eq("goods_id", goodsId));
        //有库存
        if (seckillOrder != null) {
            return seckillOrder.getOrderId();
        }
        return 0L;
    }
}
